/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2018
 */

package com.bm.console.action;

import java.util.ArrayList;
import java.util.List;

import com.bm.console.business.*;
import com.bm.console.pojo.*;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sys.object.common.ObjException;
import com.sys.object.common.ObjResultCode;
import com.sys.object.common.ObjResultException;
import com.sys.object.common.ObjResultJSON;
import com.sys.object.common.page.PageBean;

@Controller
public class WebHouseAction {

    @Resource
    WebHouseService webHouseService;

    @Resource
    WebStateService webStateService;

    @Resource
    WebCityService webCityService;

    @Resource
    WebHousePriceSectionService webHousePriceSectionService;

    @Resource
    WebHouseProjectTypeService webHouseProjectTypeService;

    @Resource
    WebHouseInvestmentTypeService webHouseInvestmentTypeService;

    @Resource
    WebHouseTypeService webHouseTypeService;

    @Resource
    WebCountryService webCountryService;

    @RequestMapping(value = "/webHouseList.do")
    public String webHouseList(PageBean pageBean, Model model, WebHouse webHouse) {
        try {
            pageBean = webHouseService.queryPageList(webHouse, pageBean);
            model.addAttribute("pageBean", pageBean);
            model.addAttribute("startRow", pageBean.getStartRow());
            model.addAttribute("entity", webHouse);

            List<WebCountry> webCountries = webCountryService.selectByAll();

            model.addAttribute("countries", webCountries);

            List<WebCityItem> city_list = new ArrayList<>();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/web/webhouse/list";
    }

    /**
     * 新增动态 Get
     */
    @RequestMapping(value = "/addWebHouse.do", method = RequestMethod.GET)
    public String addWebHouse(@ModelAttribute("webHouse") WebHouse webHouse, Model model, String id) {
        System.err.println("============id:" + id + "=========================");
        Integer prow = 0;
        try {
            List<WebState> state_list = webStateService.selectAll();

            List<WebCityItem> city_list = new ArrayList<>();


            List<WebHousePriceSection> price_section_list = webHousePriceSectionService.selectAll();
            List<WebHouseProjectType> project_type_list = webHouseProjectTypeService.selectAll();
            List<WebHouseInvestmentType> investment_type_list = webHouseInvestmentTypeService.selectAll();
            List<WebHouseType> house_type_list = webHouseTypeService.selectAll();
            if (StringUtils.hasText(id)) {
                WebHouse record = webHouseService.queryByPrimaryKey(Integer.parseInt(id));
                model.addAttribute("entity", record);
                if (record != null) {
                    if (record.getCountryId() != null) {
                        city_list = webCityService.selectByCountryId(record.getCountryId());
                    }
                }
            }
            model.addAttribute("state_list", state_list);
            model.addAttribute("city_list", city_list);
            model.addAttribute("price_section_list", price_section_list);
            model.addAttribute("project_type_list", project_type_list);
            model.addAttribute("investment_type_list", investment_type_list);
            model.addAttribute("house_type_list", house_type_list);

            List<WebCountry> webCountries = webCountryService.selectByAll();
            model.addAttribute("countries", webCountries);

        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("prow", prow);
        return "/web/webhouse/add";
    }

    /**
     * 新增动态 Post
     */
    @RequestMapping(value = "/saveWebHouse.do", method = RequestMethod.POST)
    public String saveWebHouse(@Validated @ModelAttribute("webHouse") WebHouse webHouse,
                               BindingResult result, Model model, Integer prow,
                               HttpServletRequest request) {
        System.err.println("webHouse:" + webHouse.toString());
        ObjResultException<String> ret = new ObjResultException<String>();
        try {
            int success = webHouseService.saveOrUpdate(webHouse);
            if (success > 0) {
                ret.setResultCode(ObjResultCode.OK);
            } else {
                ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
            }
        } catch (Exception e) {

            e.printStackTrace();
            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);
            }
            model.addAttribute("entity", webHouse);
            model.addAttribute("ret", ret);
            return "/web/webhouse/add";
        }
        return "redirect:/webHouseList.do?status=";
    }

    /**
     * 新增动态 Post
     */
    @RequestMapping(value = "/saveWebHouseJson.do", method = RequestMethod.POST)
    @ResponseBody
    public ObjResultException<String> saveWebHouseJson(@RequestBody WebHouse webHouse, Model model,
                                                       HttpServletRequest request) {
        ObjResultException<String> ret = new ObjResultException<String>();
        try {
            int success = webHouseService.saveOrUpdate(webHouse);
            if (success > 0) {
                ret.setResultCode(ObjResultCode.OK);
            } else {
                ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
            }
        } catch (Exception e) {
            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);
            }
        }
        return ret;
    }

    /**
     * 删除APP
     */
    @RequestMapping("/deleteWebHouse.do")
    @ResponseBody
    public ObjResultException<String> deleteWebHouse(Integer id) {
        ObjResultException<String> ret = new ObjResultException<String>();
        try {
            int success = webHouseService.delete(id);

            if (success > 0) {
                ret.setResultCode(ObjResultCode.OK);
            } else {
                ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
            }
        } catch (Exception e) {
            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);
            }
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 修改状态APP
     */
    @RequestMapping("/updateStatusWebHouse.do")
    @ResponseBody
    public ObjResultException<String> updateStatusWebHouse(WebHouse webHouse) {
        ObjResultException<String> ret = new ObjResultException<String>();
        try {
            int success = webHouseService.updateStatus(webHouse);

            if (success > 0) {
                ret.setResultCode(ObjResultCode.OK);
            } else {
                ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
            }
        } catch (Exception e) {
            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);
            }
        }
        return ret;
    }
}